package com.future.mayi.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.text.TextUtils;

import com.future.baselib.entity.DefaultResponse;
import com.future.baselib.utils.ApiCst;
import com.future.baselib.utils.HttpRequest;
import com.future.baselib.view.NoScrollViewPager;
import com.future.mayi.R;
import com.future.mayi.app.App;
import com.future.mayi.fragment.SettingPayPasswordFragment;
import com.future.mayi.fragment.VerifyPhoneFragment;
import com.future.mayi.model.interfaces.SettingPayPasswordListener;
import com.future.mayi.utils.SharedPreferencesUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class SettingPayPasswordActivity extends BaseTitleActivity implements SettingPayPasswordListener {

    @BindView(R.id.view_pager)
    NoScrollViewPager viewPager;

    private VerifyPhoneFragment verifyPhoneFragment;
    private SettingPayPasswordFragment settingPayPasswordFragment;
    private String countryId;

    @Override
    protected void initTopBar() {
        initBack();
        getTitleText().setText("修改支付密码");
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_setting_pay_password);
    }

    private List<Fragment> fragmentList;

    @Override
    protected void initView() {
        fragmentList = new ArrayList<>(2);
        verifyPhoneFragment = new VerifyPhoneFragment();
        verifyPhoneFragment.setListener(this);
        fragmentList.add(verifyPhoneFragment);
        settingPayPasswordFragment = new SettingPayPasswordFragment();
        settingPayPasswordFragment.setListener(this);
        fragmentList.add(settingPayPasswordFragment);
        viewPager.setNoScroll(true);
        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragmentList.get(position);
            }

            @Override
            public int getCount() {
                return fragmentList.size();
            }
        });

    }

    @Override
    protected void initDates() {
         countryId = (String) SharedPreferencesUtils.get(mContext, SharedPreferencesUtils.SpEnum.LoginPhoneCountryId, "");
        if (TextUtils.isEmpty(countryId)) countryId = null;
    }

    @Override
    protected void initListener() {

    }

    private String code;

    @Override
    public void edtCode(String mobile, final String code) {
        new HttpRequest<DefaultResponse>()
                .with(this)
                .addParam("token", App.getToken(mContext))
                .addParam("mobile", mobile)
                .addParam("captcha", code)
                .addParam("country_id", countryId)
                .addParam("event", "setpaypwd")
                .setApiCode(ApiCst.smsCheck)
                .setListener(new HttpRequest.OnNetworkListener<DefaultResponse>() {
                    @Override
                    public void onSuccess(DefaultResponse response) {
                        SettingPayPasswordActivity.this.code = code;
                        viewPager.setCurrentItem(1);
                    }

                    @Override
                    public void onFail(String message, DefaultResponse response) {
                        toast(message);
                    }
                })
                .start(new DefaultResponse());
    }


    @Override
    public void enterPassword(String password, String againPassword) {
        showLoadingDialog();
        new HttpRequest<DefaultResponse>()
                .with(this)
                .addParam("token", App.getToken(mContext))
                .addParam("mobile", App.getPhone())
                .addParam("paypassword", password)
                .addParam("captcha", code)
                .addParam("country_id", countryId)
                .setApiCode(ApiCst.setPayPwd)
                .setListener(new HttpRequest.OnNetworkListener<DefaultResponse>() {
                    @Override
                    public void onSuccess(DefaultResponse response) {
                        dissmissLoadingDialog();
                        finish();
                    }

                    @Override
                    public void onFail(String message, DefaultResponse response) {
                        toast(message);
                        dissmissLoadingDialog();
                        finish();
                    }
                })
                .start(new DefaultResponse());
    }

    @Override
    public void onBackPressed() {
        if (viewPager.getCurrentItem() > 0) {
            viewPager.setCurrentItem(0);
            return;
        }
        super.onBackPressed();
    }
}
